
import axios,{AxiosError, type InternalAxiosRequestConfig,type AxiosInstance,type AxiosResponse} from "axios";
import { ElNotification } from "element-plus";

const Service:AxiosInstance = axios.create({
    baseURL:"https://www.demo.com", //后期替换成环境变量
    timeout:5000, //请求超时时间
});
//请求拦截器
Service.interceptors.request.use((config:InternalAxiosRequestConfig)=>{
    return config
},(error:AxiosError)=>{
    ElNotification({
        title:"Error",
        message:"Request Error"+error.message,
        type:"error",
    });
    return Promise.reject(error)
    }
)

//响应拦截器
Service.interceptors.response.use((response:AxiosResponse)=>{
    if(response.data.code != 200){
        ElNotification({  //不打断用户操作的情况下，向用户展示一些简短的信息或提示。
            title:"Error",
            message:"请求失败"+response.data.message,
            type:"error"
        });
    }else{
        return response.data
    }
    console.log("拦截器",response)
}
,(error:AxiosError)=>{
    ElNotification({
        title:"Error",
        message:error.message,
        type:"error"
    });
    return Promise.reject(error)
})

export default Service